package com.travel.server.manager.impl.dal;

import com.travel.server.manager.dao.QuestionnaireDetailInfoDao;
import com.travel.server.manager.model.QuestionnaireDetailInfo;
import com.travel.server.manager.model.QuestionnaireDetailInfoExample;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.security.krb5.internal.PAData;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/16 18:15
 * @Desc:
 */
@Component
@Slf4j
public class QuestionnaireDetailDal {

    @Autowired
    private QuestionnaireDetailInfoDao dao;

    public QuestionnaireDetailInfo selectById(Integer id){
        QuestionnaireDetailInfo detailInfo = dao.selectByPrimaryKey(id);
        if(detailInfo == null){
            log.info("未查询到问卷详情信息, id {}", id);
        }
        return detailInfo;
    }

    public void insertOrUpdate(QuestionnaireDetailInfo questionnaireDetailInfo){
        if(questionnaireDetailInfo.getId() == null){
            dao.insert(questionnaireDetailInfo);
        }else{
            dao.updateByPrimaryKey(questionnaireDetailInfo);
        }
    }

    public List<QuestionnaireDetailInfo> selectByExample(QuestionnaireDetailParam param){
        QuestionnaireDetailInfoExample example = makeExample(param);
        return dao.selectByExample(example);
    }

    private QuestionnaireDetailInfoExample makeExample(QuestionnaireDetailParam param){
        QuestionnaireDetailInfoExample example = new QuestionnaireDetailInfoExample();
        QuestionnaireDetailInfoExample.Criteria criteria = example.or();
        if(param.getQuestionnaireId() != null){
            criteria.andQuestionnaireIdEqualTo(param.getQuestionnaireId());
        }
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        return example;
    }

    @Data
    public static class QuestionnaireDetailParam{

        private Integer questionnaireId;

        private Integer state = 1;
    }
}
